<template>
  <el-card>
    <el-row>
      <el-col :span="8">
        <el-button type="primary" size="mini" @click="dialogVisible=true">添加活动</el-button>
      </el-col>
    </el-row>
    <el-table :data="acts" highlight-current-row stripe v-loading="loading" element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading" element-loading-background="#fafafa"
      style="width: 90%; margin-top: 10px;">
      <el-table-column type="index" label="编号" align="center" width="60">
      </el-table-column>
      <el-table-column prop="actName" label="活动名称" align="center" width="200">
      </el-table-column>
      <el-table-column prop="title" label="标题" align="center" width="250">
      </el-table-column>
      <el-table-column prop="platform" label="平台" align="center" width="100">
      </el-table-column>
      <el-table-column label="主图" align="center" width="100">
        <template slot-scope="scope">
          <el-image style="width: 40px; height: 40px" :src="scope.row.image">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center" width="100">
        <template slot-scope="scope">
          <i :class="'iconfont '+ scope.row.icon" style="font-size: 40px;line-height: 40px;"></i>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteActivity(scope.row.actId)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加活动到小程序" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form label-width="100px">
        <el-form-item label="活动">
          <el-select v-model="addActivityParam.actId" filterable placeholder="请选择">
            <el-option v-for="item in sysActs" :key="item.actId" :label="item.actName" :value="item.actId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动平台">
          <el-select v-model="addActivityParam.platform" filterable placeholder="请选择">
            <el-option v-for="item in actPlatforms" :key="item.code" :label="item.msg" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称">
          <el-input v-model="addActivityParam.actName" placeholder="请输入活动名称" type="text" style="width: 70%;"></el-input>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="addActivityParam.title" placeholder="请输入活动标题" type="text" style="width: 70%;"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="addActivityParam.icon" placeholder="请输入活动图标" type="text" style="width: 70%;"></el-input>
        </el-form-item>
        <el-form-item label="主活动">
          <el-switch v-model="addActivityParam.mainAct" active-color="#13ce66" active-value=1 inactive-value=0>
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addActivity" type="warning" size="mini">添加</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
  import {
    listActivities,
    addActivity,
    deleteActivity,
    getDic
  } from '@/apis/XcxApi'
  import {
    getSysActivityList
  } from '../../apis/SysApi.js'

  export default {
    name: 'XcxActivity',
    data() {
      return {
        acts: [],
        sysActs: [],
        mainImages: [],
        dialogVisible: false,
        loading: false,
        actPlatforms: [],
        addActivityParam: {}
      }
    },
    created() {
      this.getSysActivityList()
      this.getActPlatformType()
    },
    methods: {
      listActivities() {
        listActivities().then(res => {
          if (res.data) {
            this.acts = res.data
            const actIds = this.acts.map(item => {
              return item.actId
            })
            const sysActs = this.sysActs.filter(item => {
              if (actIds.indexOf(item.actId) === -1) {
                return item
              }
            })
            this.sysActs = sysActs
          } else {
            this.acts = []
          }
        })
      },
      addActivity() {
        addActivity(this.addActivityParam).then(res => {
          if (res.data > 0) {
            this.$message.success('添加成功')
            this.addActivityParam = {
              mainAct: 0
            }
            this.changeAct()
          } else {
            this.$message.error('添加失败')
          }
        })
      },
      deleteActivity(actId) {
        deleteActivity({
          actId: actId
        }).then(res => {
          if (res.data > 0) {
            this.$message.success('删除成功')
            this.changeAct()
          } else {
            this.$message.error('删除失败')
          }
        })
      },
      getSysActivityList() {
        this.loading = true
        getSysActivityList().then(res => {
          this.sysActs = res.data
          this.loading = false
          this.changeAct()
        })
      },
      changeAct() {
        this.listActivities()
      },
      getActPlatformType() {
        getDic({
          type: 'activityPlatformType'
        }).then(res => {
          this.actPlatforms = res.data
        })
      },
      // 关闭对话框
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done()
          })
          .catch(_ => {})
      }
    }
  }
</script>

<style scoped>
</style>
